package com.xjrsoft.module.customerTwo.barin.t_barin_user_registration.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.gson.Gson;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.common.page.ConventPage;

import java.util.ArrayList;
import java.util.List;
import com.xjrsoft.common.utils.DataTransUtil;
import com.xjrsoft.common.result.Response;
import com.xjrsoft.core.secure.XjrUser;
import com.xjrsoft.core.tool.utils.BeanUtil;
import com.xjrsoft.core.tool.utils.DigestUtil;
import com.xjrsoft.core.tool.utils.StringUtil;
import com.xjrsoft.module.customerTwo.barin.brain_merchant.dto.SaveTBrainMerchantFormDataDto;
import com.xjrsoft.module.customerTwo.barin.brain_merchant.dto.TBrainMerchantDto;
import com.xjrsoft.module.customerTwo.barin.brain_merchant.entity.TBrainMerchant;
import com.xjrsoft.module.customerTwo.barin.brain_merchant.service.ITBrainMerchantService;
import com.xjrsoft.module.customerTwo.barin.brain_merchant.vo.TBrainMerchantVo;
import com.xjrsoft.module.customerTwo.barin.t_barin_user_registration.dto.XjrBaseUserDto;
import com.xjrsoft.module.customerTwo.barin.t_barin_user_registration.dto.SaveXjrBaseUserFormDataDto;
import com.xjrsoft.module.customerTwo.barin.t_barin_user_registration.entity.TBarinUser;
import com.xjrsoft.module.customerTwo.barin.t_barin_user_registration.vo.LoginUserInfoVo;
import com.xjrsoft.module.customerTwo.barin.t_barin_user_registration.vo.XjrBaseUserVo;
import com.xjrsoft.module.customerTwo.barin.t_barin_user_registration.vo.XjrBaseUserListVo;
import com.xjrsoft.module.customerTwo.barin.t_barin_user_registration.dto.XjrBaseUserListDto;
import com.xjrsoft.core.secure.utils.SecureUtil;
import com.xjrsoft.core.tool.utils.FlowableUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import com.xjrsoft.module.customerTwo.barin.t_barin_user_registration.service.ITBarinUserService;

/**
 * 用户信息表 控制器
 *
 * @author dongxinhang
 * @since 2024-04-26
 */
@RestController
@AllArgsConstructor
@RequestMapping("/t_barin_user_registration")
@Api(value = "用户注册", tags = "用户注册接口")
public class TBarinUserController {


	private final ITBarinUserService xjrBaseUserService;

	private final ITBrainMerchantService tBrainMerchantService;


	/**
	 * 获取用户详细信息
	 */
	@GetMapping("/{id}")
	@ApiOperation(value = "详情", notes = "传入xjrBaseUser")
	@SaCheckPermission(value = {"t_barin_user_registration:view", "t_barin_user_registration:modify"}, mode = SaMode.OR)
	public Response<XjrBaseUserVo> getFormData(@PathVariable String id) {
		// 主表数据
		TBarinUser TBarinUser = xjrBaseUserService.getById(id);
		XjrBaseUserVo xjrBaseUserVo = BeanUtil.copy(TBarinUser, XjrBaseUserVo.class);
		return Response.ok(xjrBaseUserVo);
	}

	/**
	 * 自定义分页 用户信息表
	 */
	@GetMapping
	@ApiOperation(value = "分页", notes = "传入xjrBaseUser")
		public Response<PageOutput<XjrBaseUserListVo>> getPageList(XjrBaseUserListDto listDto) {
		IPage<TBarinUser> page = xjrBaseUserService.getPageList(listDto);
		List<XjrBaseUserListVo> records = BeanUtil.copyList(page.getRecords(), XjrBaseUserListVo.class);
		// 转换列表数据
		DataTransUtil.transListShowData(records);
		return Response.ok(ConventPage.getPageOutput(page.getTotal(), records));
	}

	/**
	 * 新增 用户信息表（用户注册）
	 */
	@PostMapping
	@ApiOperation(value = "新增", notes = "传入xjrBaseUser")
	public Response save(@RequestBody SaveXjrBaseUserFormDataDto formDto) {
		XjrBaseUserDto xjrBaseUserDto = formDto.getXjrBaseUserDto();
		TBarinUser tBarinUser = BeanUtil.copy(xjrBaseUserDto, TBarinUser.class);
		tBarinUser.setFRealname(tBarinUser.getFAccount());
		if(StringUtils.isNotBlank(tBarinUser.getFMobile())){
			tBarinUser.setFTelephone(tBarinUser.getFMobile());
		}
		if(StringUtils.isNotBlank(tBarinUser.getFTelephone())){
			tBarinUser.setFMobile(tBarinUser.getFTelephone());
		}
		//检查账号和手机号唯一性
		xjrBaseUserService.checkUser(tBarinUser.getFAccount(),tBarinUser.getFMobile(),"");
		// 密码MD5加密
		String password = tBarinUser.getFPassword();
		if (!StringUtil.isEmpty(password)) {
			tBarinUser.setFPassword(DigestUtil.encrypt(password));
		}
		tBarinUser.setFDeletemark(0);
		tBarinUser.setFEnabledmark(1);	//默认可以登录
		String dataId = xjrBaseUserService.addXjrBaseUser(tBarinUser);
		//如果是服务商注册，新增一条商户数据
		if(StringUtils.isNotBlank(dataId)&&"1".equals(tBarinUser.getFUsertype())){
			TBrainMerchant tBrainMerchant = new TBrainMerchant();
			tBrainMerchant.setFMerchantid(dataId);
			tBrainMerchantService.save(tBrainMerchant);
		}
		return dataId == null ? Response.notOk() : Response.ok(dataId);
	}

	/**
	 * 修改 用户信息表（用户信息修改）
	 */
	@PutMapping("/{id}")
	@ApiOperation(value = "修改", notes = "传入xjrBaseUser")
	public Response update(@PathVariable String id, @RequestBody XjrBaseUserDto xjrBaseUserDto) {
		TBarinUser tBarinUser = BeanUtil.copy(xjrBaseUserDto, TBarinUser.class);
		tBarinUser.setFRealname(tBarinUser.getFAccount());
		if(StringUtils.isNotBlank(tBarinUser.getFMobile())){
			tBarinUser.setFTelephone(tBarinUser.getFMobile());
		}
		if(StringUtils.isNotBlank(tBarinUser.getFTelephone())){
			tBarinUser.setFMobile(tBarinUser.getFTelephone());
		}
		//检查账号和手机号唯一性
		xjrBaseUserService.checkUser(tBarinUser.getFAccount(),tBarinUser.getFMobile(),id);
		return Response.status(xjrBaseUserService.updateXjrBaseUser(id, tBarinUser));
	}



	/**
	 * 删除 用户信息表
	 */
	@DeleteMapping("/{ids}")
	@ApiOperation(value = "删除", notes = "传入ids")
	@SaCheckPermission(value = {"t_barin_user_registration:batchDelete", "t_barin_user_registration:delete"}, mode = SaMode.OR)
	public Response remove(@ApiParam(value = "主键集合", required = true) @PathVariable String ids) {
		return Response.status(xjrBaseUserService.delete(ids));
	}

	/**
	 * 服务商管理-基本信息-获取账号基本信息
	 */
	@GetMapping("/getLoginUserInfo")
	@ApiOperation(value = "详情", notes = "传入xjrBaseUser")
	public Response<LoginUserInfoVo> getLoginUserInfo() {
		// 主表数据
		TBarinUser TBarinUser = xjrBaseUserService.getById(SecureUtil.getUser().getUserId());
		LoginUserInfoVo loginUserInfoVo = BeanUtil.copy(TBarinUser, LoginUserInfoVo.class);
		List<LoginUserInfoVo> list = new ArrayList<>();
		list.add(loginUserInfoVo);
		DataTransUtil.transListShowData(list);
		return Response.ok(loginUserInfoVo);
	}

	/**
	 * 服务商管理-基本信息-获取商户店铺基本信息
	 */
	@GetMapping("/getLoginUserShopInfo")
	@ApiOperation(value = "详情", notes = "传入xjrBaseUser")
	public Response<TBrainMerchantVo> getLoginUserShopInfo() {
		// 主表数据
		TBrainMerchant tBrainMerchant = tBrainMerchantService.getById(SecureUtil.getUser().getMerchantid());
		TBrainMerchantVo tBrainMerchantVo = BeanUtil.copy(tBrainMerchant, TBrainMerchantVo.class);
		return Response.ok(tBrainMerchantVo);
	}

	/**
	 * 修改
	 */
	@PutMapping("/updateLoginUserShopInfo")
	@ApiOperation(value = "修改", notes = "传入tBrainMerchant")
	public Response update(@RequestBody TBrainMerchantDto tBrainMerchantDto) {
		TBrainMerchant tBrainMerchant = BeanUtil.copy(tBrainMerchantDto, TBrainMerchant.class);
		return Response.status(tBrainMerchantService.updateTBrainMerchant(SecureUtil.getUser().getMerchantid(), tBrainMerchant));
	}
}
